package com.logos.musicframe.controller;

import com.logos.musicframe.req.PlaylistCollectedReq;
import com.logos.musicframe.resp.PlaylistResp;
import com.logos.musicframe.result.Result;
import com.logos.musicframe.service.PlaylistCollectedService;
import com.logos.musicframe.utils.UserInfoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 * (PlaylistCollected)表控制层
 *
 * @author logos
 * @since 2025-06-15 12:32:44
 */
@Slf4j
@Component
public class PlaylistCollectedController {
    /**
     * 服务对象
     */
    @Autowired
    private PlaylistCollectedService playlistCollectedService;

    @Autowired
    private UserInfoUtil userInfoUtil;

    /**
     * 收藏歌单
     */
    public Result<Boolean> collect(Integer playlistId) {
        try {
            log.info("PlaylistCollectedController.collect.playlistId:{}", playlistId);
            // 拿到当前登录用户
            Integer userId = userInfoUtil.getUserId();
            PlaylistCollectedReq playlistCollectedReq = new PlaylistCollectedReq()
                    .setPlaylistId(playlistId)
                    .setUserId(userId);
            Boolean result = playlistCollectedService.collect(playlistCollectedReq);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("PlaylistCollectedController.collect.error:{}", e.getMessage(), e);
            return Result.fail("收藏歌单失败");
        }
    }

    /**
     * 取消收藏歌单
     */
    public Result<Boolean> unCollect(Integer playlistId) {
        try {
            log.info("PlaylistCollectedController.unCollect.playlistId:{}", playlistId);
            // 拿到当前登录用户
            Integer userId = userInfoUtil.getUserId();
            PlaylistCollectedReq playlistCollectedReq = new PlaylistCollectedReq()
                    .setPlaylistId(playlistId)
                    .setUserId(userId);
            Boolean result = playlistCollectedService.unCollect(playlistCollectedReq);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("PlaylistCollectedController.unCollect.error:{}", e.getMessage(), e);
            return Result.fail("取消收藏歌单失败");
        }
    }

    /**
     * 查询当前用户收藏的歌单
     */
    public Result<List<PlaylistResp>> queryCollectedPlaylist() {
        try {
            Integer userId = userInfoUtil.getUserId();
            List<PlaylistResp> result = playlistCollectedService.queryByUserId(userId);
            return Result.ok(result);
        }catch (Exception e){
            log.error("PlaylistCollectedController.queryByUserId.error:{}",e.getMessage(),e);
            return Result.fail("查询收藏歌单失败");
        }
    }


}

